home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap02.txt < prev    next >
Text File  |  1992-01-18  |  10KB  |  235 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 2
  8.                                                    COMPOUND TYPES
  9.  
  10. ENUMERATED TYPES
  11. _________________________________________________________________
  12.  
  13. Examine the file named ENUM.CPP for an example     ==============
  14. that uses an enumerated type variable.  The           ENUM.CPP
  15. enumerated type is used in C++ in exactly the      ==============
  16. same way it was used in ANSI-C with one small
  17. exception, the keyword enum is not required to
  18. be used again when defining a variable of that type, but it can be
  19. used if desired.  It may be clearer to you to use the keyword when
  20. defining a variable in the same manner that it is required to be
  21. used in C, and you may choose to do so.
  22.  
  23. The example program uses the keyword enum in line 9, but omits it
  24. in line 8 to illustrate to you that it is indeed optional.  The
  25. remainder of this program should be no problem for you to
  26. understand.  After studying it, be sure to compile and execute it
  27. and examine the output.
  28.  
  29.  
  30.  
  31. A SIMPLE STRUCTURE
  32. _________________________________________________________________
  33.  
  34. Examine the example program named STRUCTUR.CPP   ================
  35. for an illustration using a very simple            STRUCTUR.CPP
  36. structure.  This structure is no different from  ================
  37. that used in ANSI-C except for the fact that the
  38. keyword struct is not required to be used again
  39. when defining a variable of that type.  Lines 11 and 12 illustrate
  40. the declaration of variables without the keyword, and line 13
  41. indicates that the keyword struct can be included if desired.  It
  42. is up to you to choose which style you prefer to use in your C++
  43. programs.
  44.  
  45. You should take note of the fact that this is a valid ANSI-C
  46. program except for the fact that it uses the stream library, the
  47. C++ comments, and the lack of use of the keyword struct in two of
  48. the lines.
  49.  
  50.  
  51. Once again, be sure to compile and execute this program after
  52. studying it carefully, because the next example program is very
  53. similar but it introduces a brand new construct not available in
  54. standard C, the class.
  55.  
  56.  
  57.  
  58.                                                          Page 2-1
  59.  
  60.                                        Chapter 2 - Compound Types
  61.  
  62.  
  63. A VERY SIMPLE CLASS
  64. _________________________________________________________________
  65.  
  66. Examine the example program named CLASS1.CPP for   ==============
  67. our first example of a class in C++.  This is        CLASS1.CPP
  68. the first class example, but it will not be the    ==============
  69. last, since the class is the major reason for
  70. using C++ over ANSI-C or some other programming
  71. language.  You will notice the keyword class used in line 4, in
  72. exactly the same way that the keyword struct was used in the last
  73. program, and they are in fact very similar constructs.  There are
  74. definite differences, as we will see, but for the present time we
  75. will be concerned more with their similarities.
  76.  
  77. The word animal in line 4 is the name of the class, and when we
  78. declare variables of this type in lines 12 through 14, we can
  79. either omit the keyword class or include it if we desire as
  80. illustrated in line 14.  In the last program, we declared 5
  81. variables of a structure type, but in this program we declare 5
  82. objects.  They are called objects because they are of a class type.
  83. The differences are subtle, and in this case the differences are
  84. negligible, but as we proceed through this tutorial, we will see
  85. that the class construct is indeed very important and valuable.
  86. The class was introduced here only to give you a glimpse of what
  87. is to come later in this tutorial.
  88.  
  89. The class is a type which can be used to declare objects in much
  90. the same way that a structure is a type that can be used to declare
  91. variables.  Your dog named King is a specific instance of the
  92. general class of dogs, and in a similar manner, an object is a
  93. specific instance of a class.  It would be well to take note of the
  94. fact that the class is such a generalized concept that there will
  95. be libraries of prewritten classes available in the marketplace
  96. soon.  You will be able to purchase classes which will perform some
  97. generalized operations such as managing stacks, queues, or lists,
  98. sorting data, managing windows, etc.  This is because of the
  99. generality and flexibility of the class construct.  In fact, a few
  100. class libraries are available now.
  101.  
  102. The new keyword public in line 5, followed by a colon, is necessary
  103. in this case because the variables in a class are defaulted to a
  104. private type and we could not access them at all without making
  105. them public.  Don't worry about this program yet, we will cover all
  106. of this in great detail later in this tutorial.
  107.  
  108. Be sure to compile and run it to see that it does what we say it
  109. does with your compiler.  Keep in mind that this is your first
  110. example of a class and it illustrates essentially nothing
  111. concerning the use of this powerful C++ construct.
  112.  
  113.  
  114.  
  115.  
  116.  
  117.                                                          Page 2-2
  118.  
  119.                                        Chapter 2 - Compound Types
  120.  
  121.  
  122. THE FREE UNION OF C++
  123. _________________________________________________________________
  124.  
  125. Examine the program named UNIONEX.CPP for an      ===============
  126. example of a free union.  In ANSI-C, all unions     UNIONEX.CPP
  127. must be named in order to be used, but this is    ===============
  128. not true in C++.  When using C++ we can use a
  129. free union, a union without a name.  The union
  130. is embedded within a simple structure and you will notice that
  131. there is not a variable name following the declaration of the union
  132. in line 11.  In ANSI-C, we would have to name the union and give
  133. a triple name (three names dotted together) to access the members.
  134. Since it is a free union, there is no union name, and the variables
  135. are accessed with only a doubly dotted name as illustrated in lines
  136. 18, 22, 26, 28, and 29.
  137.  
  138. You will recall that a union causes all the data contained within
  139. the union to be stored in the same physical memory locations, such
  140. that only one variable is actually available at a time.  This is
  141. exactly what is happening here.  The variable named fuel_load,
  142. bomb_load, and pallets are stored in the same physical memory
  143. locations and it is up to the programmer to keep track of which
  144. variable is stored there at any given time.  You will notice that
  145. the transport is assigned a value for pallets in line 26, then a
  146. value for fuel_load in line 28.  When the value for fuel_load is
  147. assigned, the value for pallets is corrupted and is no longer
  148. available since it was stored where fuel_load is currently stored.
  149. The observant student will notice that this is exactly the way the
  150. union is used in ANSI-C except for the way components are named.
  151.  
  152. The remainder of the program should be easy for you to understand,
  153. so after you study it and understand it, compile and execute it.
  154.  
  155.  
  156. C++ TYPE CONVERSIONS
  157. _________________________________________________________________
  158.  
  159. Examine the program named TYPECONV.CPP for a few ================
  160. examples of type conversions in C++.  The type     TYPECONV.CPP
  161. conversions are done in C++ in exactly the same  ================
  162. manner as they are done in ANSI-C, but C++ gives
  163. you another form for doing the conversions.
  164.  
  165. Lines 10 through 18 of this program use the familiar "cast" form
  166. of type conversions used in ANSI-C, and there is nothing new to the
  167. experienced C programmer.  You will notice that lines 10 through
  168. 13 are actually all identical to each other.  The only difference
  169. is that we are coercing the compiler to do the indicated type
  170. conversions prior to doing the addition and the assignment in some
  171. of the statements.  In line 13, the int type variable will be
  172. converted to type float prior to the addition, then the resulting
  173. float will be converted to type char prior to being assigned to the
  174. variable c.
  175.  
  176.                                                          Page 2-3
  177.  
  178.                                        Chapter 2 - Compound Types
  179.  
  180.  
  181. Additional examples of type coercion are given in lines 15 through
  182.